package com.deram.ctin.serv.enter.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.deram.ctin.common.bean.PageResult;
import com.deram.ctin.common.consts.ErrorConsts;
import com.deram.ctin.common.error.BusinessException;
import com.deram.ctin.common.utils.ArrayHandleUtils;
import com.deram.ctin.common.utils.BusinessUtils;
import com.deram.ctin.common.utils.ValidateUtils;
import com.deram.ctin.dependencies.serv.utils.MybatisUtils;
import com.deram.ctin.serv.enter.entity.BaseUpdateInfo;
import com.deram.ctin.serv.enter.entity.EnterInfo;
import com.deram.ctin.serv.enter.entity.EnterRecruit;
import com.deram.ctin.serv.enter.filter.EnterRecruitFilter;
import com.deram.ctin.serv.enter.mapper.EnterRecruitMapper;
import com.deram.ctin.serv.enter.service.EnterInfoService;
import com.deram.ctin.serv.enter.service.EnterRecruitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: jtt
 * @Description:
 * @Date: 2017/9/12 15:34
 * @version: V1.0
 */
@Service
public class EnterRecruitServiceImpl implements EnterRecruitService {

    @Autowired
    EnterRecruitMapper enterRecruitMapper;

    @Autowired
    EnterInfoService enterInfoService;

    @Override
    public EnterRecruit find(Long id) {
        ValidateUtils.isId(id, "招聘ID");
        EnterRecruit recruit = enterRecruitMapper.selectById(id);
        BusinessUtils.notNull(recruit, "招聘信息不存在");
        return recruit;
    }

    @Override
    public Page<EnterRecruit> search(EnterRecruit filter, Pagination pagination) {
//        PageHelper.startPage(pagination.getCurrent(), pagination.getSize());
//        Page<EnterRecruit> result = new Page<>();
//        result.setRecords(list(filter));
        return null;
    }

    @Override
    public List<EnterRecruit> list(EnterRecruitFilter filter) {
        ValidateUtils.isId(filter.getEnterId(), "企业ID");
        EntityWrapper<EnterRecruit> ew = createEntityWrapper(filter);
        List<EnterRecruit> result = enterRecruitMapper.selectList(ew);
        return result;
    }

    @Override
    public EnterRecruit save(EnterRecruit param) {
        ValidateUtils.isId(param.getEnterId(), "企业ID");
        if (null != param.getId()) {
            EnterRecruit finder = find(param.getId());
            enterRecruitMapper.updateById(param);
        } else {
            enterRecruitMapper.insert(param);
        }
        return param;
    }

    @Override
    public void delete(Long id) {
        enterRecruitMapper.deleteById(find(id));
    }

    @Override
    public void deleteByEnterId(Long id) {
        EnterRecruitFilter filter = new EnterRecruitFilter();
        filter.setEnterId(id);
        List<EnterRecruit> lists = list(filter);
        if (null != lists && !lists.isEmpty()) {
            List<Long> ids = ArrayHandleUtils.readAttrList(lists, "id");
            enterRecruitMapper.deleteBatchIds(ids);
        }
    }

    @Override
    public void saveList(BaseUpdateInfo<EnterRecruit> updateInfo) {
        Long enterId = updateInfo.getEnterId();
        EnterInfo enterInfo = enterInfoService.find(enterId);
        if (null == enterInfo) {
            throw new BusinessException(ErrorConsts.VALIDATE, "工商信息未找到");
        }
        deleteByEnterId(enterId);
        List<EnterRecruit> lists = updateInfo.getList();
        for (EnterRecruit item : lists) {
            item.setId(null);
            item.setEnterId(enterId);
        }
        enterRecruitMapper.batchInsert(lists);
    }

    @Override
    public PageResult<EnterRecruit> search(EnterRecruitFilter filter, Integer pageNo, Integer pageSize) {
        Pagination pagination = MybatisUtils.createPagination(pageNo,pageSize);

        EntityWrapper<EnterRecruit> entityWrapper = createEntityWrapper(filter);
        return MybatisUtils.createPageResult(enterRecruitMapper.selectPage(pagination,entityWrapper),pagination);
    }

    private EntityWrapper<EnterRecruit> createEntityWrapper(EnterRecruitFilter filter) {
        EnterRecruit entity = new EnterRecruit();
        entity.setEnterId(filter.getEnterId());
        EntityWrapper<EnterRecruit> wrapper = new EntityWrapper<>(entity);
        return wrapper;
    }
}
